package com.springboot.config.redis;

import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.PostConstruct;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;



import redis.clients.jedis.Tuple;

@Component
public class CacheService {

	private static final String PLATFORM_KEY_PRE = "DTB_";
	
	//@Autowired
	//private CacheClusterService cacheClusterService;
	
	@Autowired
	private CacheSingleService cacheSingleService;
	
	private ICacheService cacheService;
	
	@PostConstruct
	private void init(){
		/*if("pro".equals(KeegooConfig.RUN_MODEL)){
			this.cacheService = cacheClusterService;
		}else{
			this.cacheService = cacheSingleService;
		}*/
		this.cacheService = cacheSingleService;
	}

	protected String getStrKey(String key){
		String result = PLATFORM_KEY_PRE+key;
		return result;
	}
	
	/**
	 * 
	 * setex(写入缓存)
	 * @param key 
	 * @param seconds 过期时间 单位秒
	 * @param value
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void setex(String key, int seconds,String value){
		key = getStrKey(key);
		cacheService.setex(key, seconds, value);
	}
	
	public void setexNotPre(String key, int seconds,String value){
		cacheService.setex(key, seconds, value);
	}
	
	public void set(String key, String value) throws Exception{
		key = getStrKey(key);
		cacheService.set(key, value);
	}
	
	/**
	 * 
	 * get(返回指定key值)
	 * @param key
	 * @return
	 * String
	 * @exception
	 * @since  1.0.0
	 */
	public String get(String key){
		key = getStrKey(key);
		return cacheService.get(key);
	}
	
	public String getNotPre(String key){
		return cacheService.get(key);
	}
	
	/**
	 * @param key
	 * @return Set<String>
	 * @exception
	 * @since  1.0.0
	 */
	public Set<String> keys(String key){
		return cacheService.keys(key);
	}
	
	public Set<String> keys(String key, String keyPre){
		if(!StringUtils.isEmpty(keyPre)){
			key = keyPre+key;
		}
		return cacheService.keys(key);
	}
	
	/**
	 * get(返回指定key值)
	 * @param key
	 * @return
	 * String
	 * @exception
	 * @since  1.0.0
	 */
	public List<String> mget(String...params){
		return cacheService.mget(params);
	}
	
	/**
	 * 判断是否有某个key
	 * @param key
	 * @return
	 * String
	 * @exception
	 * @since  1.0.0
	 */
	public boolean exists(String key){
		key = getStrKey(key);
		return cacheService.exists(key);
	}
	
	
	
	/**
	 * get(返回指定key值)
	 * @param key
	 * @return
	 * String
	 * @exception
	 * @since  1.0.0
	 */
	public void del(String key){
		key = getStrKey(key);
		cacheService.del(key);
	}
	
	public void del(String key,String keyPre){
		if(!StringUtils.isEmpty(keyPre)){
			key = keyPre+key;
		}
		cacheService.del(key);
	}
	
	public long dels(String... keys){
		return cacheService.dels(keys);
	}
	
	
	/**
	 * push(在指定链表头部增加一个或多个值，链表不存在则新建)
	 * @param queueName 队列名
	 * @param values 值顺序列表，如：a,b,c.先进先出
	 * @return Long 队列元素个数
	 * @exception
	 * @since  1.0.0
	 */
	public Long push(String queueName,String... values){
		return cacheService.push(queueName, values);
	}
	
	/**
	 * 
	 * pop(从指定队列表尾部取出一个值)
	 * @param queueName 队列名
	 * @return String 元素值
	 * @exception
	 * @since  1.0.0
	 */
	public String pop(String queueName){
		return cacheService.pop(queueName);
	}
	
	/**
	 * lrange(从指定队列取起始位置到结束位置元素)
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 * List<String>
	 * @exception
	 * @since  1.0.0
	 */
	public List<String> lrange(String key,long start, long end){
		key = getStrKey(key);
		return cacheService.lrange(key, start, end);
	}
	
	public List<String> lrangeNotPre(String key,long start, long end){
		return cacheService.lrange(key, start, end);
	}
	
	
	/**
	 * lrange(从指定队列取起始位置到结束位置元素)
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 * List<String>
	 * @exception
	 * @since  1.0.0
	 */
	public long llen(String key){
		key = getStrKey(key);
		return cacheService.llen(key);
	}
	
	public void hset(String key,String field, String value){
		key = getStrKey(key);
		cacheService.hset(key, field, value);
	}
	
	public void hsetNotPre(String key,String field, String value){
		cacheService.hset(key, field, value);
	}
	
	public String hget(String key,String field){
		key = getStrKey(key);
		return cacheService.hget(key, field);
	}
	
	public String hgetNotPre(String key,String field){
		return cacheService.hget(key, field);
	}
	
	public Map<String,String> hget(String key){
		key = getStrKey(key);
		return cacheService.hget(key);
	}
	
	public List<String> hmget(String key,String...fields){
		key = getStrKey(key);
		return cacheService.hmget(key, fields);
	} 
	
	public Long hdel(String key,String...fields){
		key = getStrKey(key);
		return cacheService.hdel(key, fields);
	}
	
	public Long hdel(String key,String field){
		key = getStrKey(key);
		return cacheService.hdel(key, field);
	} 
	
	/**
	 * queueLength(返回指定队列元素个数)
	 * @param queueName
	 * @return Long
	 * @exception
	 * @since  1.0.0
	 */
	public Long queueLength(String queueName){
		return cacheService.queueLength(queueName);
	}
	
	/**
	 * 
	 * zadd(向有序sorts-set里添加值)
	 * @param key 指定key
	 * @param score 分数，排序用
	 * @param member 成员(要存的值)
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void zadd(String key,double score,String member){
		key = getStrKey(key);
		cacheService.zadd(key, score, member);
	}
	
	/**
	 * sadd	集合(Set)	(向集合添加一个或多个成员)
	 * @param key
	 * @param member
	 */
	public void sadd(String key, String member){
		key = getStrKey(key);
		cacheService.sadd(key, member);
	}
	

	/**
	 * SET 集合
	 * 判断成员元素是否是集合的成员
	 * @param key
	 * @param member
	 * @return
	 */
	public Boolean sismember(String key, String member) {
		key = getStrKey(key);
		return cacheService.sismember(key, member);
	}
	
	/**
	 * SET 集合
	 * 获取集合的成员
	 * @param key
	 * @param member
	 * @return
	 */
	public Set<String> smembers(String key) {
		key = getStrKey(key);
		return cacheService.smembers(key);
	}
	
	/**
	 * SET 集合
	 * 获取集合的成员
	 * @param key
	 * @param member
	 * @return
	 */
	public Set<String> sinter(String... keys) {
		return cacheService.sinter(keys);
	}
	
	/**
	 * 
	 * zincress(向有序sorts-set里添加值)
	 * @param key 指定key
	 * @param score 分数，排序用
	 * @param member 成员(要存的值)
	 * void
	 * @exception
	 * @since  1.0.0
	 */
	public void zincress(String key,double score,String member){
		key = getStrKey(key);
		cacheService.zincress(key, score, member);
	}
	
	/**
	 * 
	 * zcard(返回key Sets数量)
	 * @param key
	 * @return
	 * Long
	 * @exception
	 * @since  1.0.0
	 */
	public Long zcard(String key){
		key = getStrKey(key);
		return cacheService.zcard(key);
	}
	
	/**
	 * 
	 * zrange(返回有序list，以score正序排，1，2，3，4等 )
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 * List<String>
	 * @exception
	 * @since  1.0.0
	 */
	public Set<String> zrange(String key,final long start,final long end){
		key = getStrKey(key);
		return cacheService.zrange(key, start, end);
	}
	
	public List<Tuple> zrangeWithScores(String key,final long start,final long end){
		key = getStrKey(key);
		return cacheService.zrangeWithScores(key, start, end);
	}
	
	public Long zrevrank(String key, String member) {
		key = getStrKey(key);
		return cacheService.zrevrank(key, member);
	}
	
	/**
	 * 
	 * zrevrangeByScore(返回有续集key中score<=max并且score>=min 的元素，返回结果根据score从大到小顺序排列 )
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 * List<String>
	 * @exception
	 * @since  1.0.0
	 */
	public List<String> zrevrangeByScore(String key,final double min,final double max, int skip, int size){
		key = getStrKey(key);
		return cacheService.zrevrangeByScore(key, min, max, skip, size);
	}
	
	/**
	 *  zrevrange 命令返回有序集中，指定区间内的成员。
	 *  其中成员的位置按分数值递减(从大到小)来排列。
	 *  具有相同分数值的成员按字典序的逆序(reverse lexicographical order)排列。
	 *  除了成员按分数值递减的次序排列这一点外，
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 */
	public Set<Tuple> zrevrangeWithScorese(String key,final long start,final long end){
		key = getStrKey(key);
		return cacheService.zrevrangeWithScorese(key, start, end);
	}
	
	
	public void zrem(String key,String... members){
		key = getStrKey(key);
		cacheService.zrem(key, members);
	}
	
	/**
	 * zscore(返回某个成员的分数，如果不存在返回null)
	 * @param key
	 * @param member
	 * @return
	 * Double
	 * @exception
	 * @since  1.0.0
	 */
	public Double zscore(String key,String member){
		key = getStrKey(key);
		return cacheService.zscore(key, member);
	}
	
	/**
	 * 有序集合中指定成员的分数加上增量
	 * @param key
	 * @param score
	 * @param member
	 * @return
	 */
	public Double zincrby(String key, double score, String member){
		key = getStrKey(key);
		return cacheService.zincrby(key, score, member);
	}
	
	/**
	 * incr(按key自增(+1))
	 * @param key
	 * @return
	 * Long
	 * @exception
	 * @since  1.0.0
	 */
	public Long incr(String key){
		key = getStrKey(key);
		return cacheService.incr(key);
	}
	
	public Long incrBy(String key, long increament){
		key = getStrKey(key);
		return cacheService.incrBy(key, increament);
	}
	
	/**
	 * redis锁
	 * @param key
	 * @param value
	 * @return
	 */
	public Long setnx(String key,String value){
		key = getStrKey(key);
		return cacheService.setnx(key, value);
	}

	/**
	 * 存放List
	 * @param key
	 * @param value
	 * @return
	 */
	public Long rpush(String key, String value) {
		return cacheService.rpush(key, value);
	}
	/**
	 * 获取List
	 * @param key
	 * @param start 
	 * @param count -1为全部
	 * @return
	 */
	public List<String> sort(String key,int start,int count) {
		return cacheService.sort(key, start, count);
	}
	
	/**
	 * 发布
	 * @param channel
	 * @param message
	 * @return
	 */
	public Long publish(String channel,String message) {
		return cacheService.publish(channel, message);
	}
	
	/**
	 * 获取值过期时间
	 * @param key
	 * @return
	 */
	public Long ttl(String key) {
		key = getStrKey(key);
		return cacheService.ttl(key);
	}
	
	public Long smove(String srckey, String dstkey, String member){
		srckey = getStrKey(srckey);
		dstkey = getStrKey(dstkey);
		return cacheService.smove(srckey, dstkey, member);
	}
	
	public boolean hincrBy(String key,String field,long value){
		key = getStrKey(key);
		return cacheService.hincrBy(key, field, value);
	}
	
	public Long scard(String key){
		key = getStrKey(key);
		return cacheService.scard(key);
	}
	
	public List<String >srandmember(String key,int count){
		key = getStrKey(key);
		return cacheService.srandmember(key, count);
	}

	public long hlen(String key) {
		key = getStrKey(key);
		return cacheService.hlen(key);
	}

	public Long srem(String key,String member){
		key = getStrKey(key);
		return cacheService.srem(key,member);
	}
}
